package com.qsw.refreshrecyclerview.RefreshRecyclerView.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.qsw.refreshrecyclerview.RefreshRecyclerView.adapter.ScrollTableViewAdapter;
import com.qsw.refreshrecyclerview.RefreshRecyclerView.listener.OnItemClickListener;
import com.qsw.refreshrecyclerview.RefreshRecyclerView.listener.RecyclerTouchListener;

/**
 * 锁定首列首行的表格布局
 * <p>
 * 整体界面分为四部分
 * 左上：属于标题的首列，无论左右还是上下滑动，该部分始终固定
 * 右上：属于除首列后的标题，会随着右下数据部分左右滑动，当上下滑动时该布局固定
 * 左下：属于数据的首列，会随着右下数据上下滑动，当左右滑动时该布局固定
 * 右下：数据除首列后的数据，会上下左右滑动
 */
public class ScrollTableView extends LinearLayout {

    private LinearLayout mLeftRootContainer;
    private HorizontalScrollView mRightRootContainer;
    private LinearLayout mRightLLContainer;
    private RecyclerView mLeftBottomRv;
    private RecyclerView mRightBottomRv;

    private ScrollTableViewAdapter mScrollTableViewAdapter;

    private RecyclerTouchListener mLeftRecyclerTouchListener;
    private RecyclerTouchListener mRightRecyclerTouchListener;

    private int mMaxItemHeight;

    public ScrollTableView(Context context) {
        super(context);
        init();
    }

    public ScrollTableView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ScrollTableView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(HORIZONTAL);

        mLeftRootContainer = new LinearLayout(getContext());
        mLeftRootContainer.setOrientation(VERTICAL);
        mLeftRootContainer.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        mRightRootContainer = new HorizontalScrollView(getContext());
        mRightRootContainer.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        mRightLLContainer = new LinearLayout(getContext());
        mRightLLContainer.setOrientation(VERTICAL);
        mRightLLContainer.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        addView(mLeftRootContainer);
        addView(mRightRootContainer);
        mRightRootContainer.addView(mRightLLContainer);

        mLeftBottomRv = new RecyclerView(getContext());
        mLeftBottomRv.setLayoutManager(new LinearLayoutManager(getContext()) {
            @Override
            public void measureChildWithMargins(View child, int widthUsed, int heightUsed) {
                if (mMaxItemHeight < child.getMeasuredHeight()) {
                    mMaxItemHeight = child.getMeasuredHeight();
                }
                if (mMaxItemHeight > 0) {
                    ViewGroup.LayoutParams layoutParams = child.getLayoutParams();
                    layoutParams.height = mMaxItemHeight;
                    child.setLayoutParams(layoutParams);
                }
                super.measureChildWithMargins(child, widthUsed, heightUsed);
            }
        });
        mLeftBottomRv.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mLeftBottomRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
                    mRightBottomRv.scrollBy(dx, dy);
                }
            }
        });
        if (mLeftRecyclerTouchListener != null) {
            mLeftBottomRv.removeOnItemTouchListener(mLeftRecyclerTouchListener);
        }
        mLeftRecyclerTouchListener = new RecyclerTouchListener(getContext(), mLeftBottomRv, null);
        mLeftBottomRv.addOnItemTouchListener(mLeftRecyclerTouchListener);
        mLeftBottomRv.setAdapter(mLeftBottomRVAdapter);

        mRightBottomRv = new RecyclerView(getContext());
        mRightBottomRv.setLayoutManager(new LinearLayoutManager(getContext()) {
            @Override
            public void measureChildWithMargins(View child, int widthUsed, int heightUsed) {
                if (mMaxItemHeight < child.getMeasuredHeight()) {
                    mMaxItemHeight = child.getMeasuredHeight();
                }
                if (mMaxItemHeight > 0) {
                    ViewGroup.LayoutParams layoutParams = child.getLayoutParams();
                    layoutParams.height = mMaxItemHeight;
                    child.setLayoutParams(layoutParams);
                }
                super.measureChildWithMargins(child, widthUsed, heightUsed);
            }
        });
        mRightBottomRv.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mRightBottomRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
                    mLeftBottomRv.scrollBy(dx, dy);
                }
            }
        });
        if (mRightRecyclerTouchListener != null) {
            mRightBottomRv.removeOnItemTouchListener(mRightRecyclerTouchListener);
        }
        mRightRecyclerTouchListener = new RecyclerTouchListener(getContext(), mRightBottomRv, null);
        mRightBottomRv.addOnItemTouchListener(mRightRecyclerTouchListener);
        mRightBottomRv.setAdapter(mRightBottomRVAdapter);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        ViewGroup.LayoutParams leftRootParams = mLeftRootContainer.getLayoutParams();
        leftRootParams.width = (int) (getMeasuredWidth() * mScrollTableViewAdapter.onGetLeftWidthPercent());
        leftRootParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        mLeftRootContainer.setLayoutParams(leftRootParams);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setAdapter(ScrollTableViewAdapter scrollTableViewAdapter) {
        mScrollTableViewAdapter = scrollTableViewAdapter;

        mLeftRootContainer.removeAllViews();
        mRightLLContainer.removeAllViews();
        if (mScrollTableViewAdapter != null) {
            if (mScrollTableViewAdapter.onGetLeftTopLayout() != null) {
                mLeftRootContainer.addView(mScrollTableViewAdapter.onGetLeftTopLayout(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            }
            mLeftRootContainer.addView(mLeftBottomRv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            if (mScrollTableViewAdapter.onGetRightTopLayout() != null) {
                mRightLLContainer.addView(mScrollTableViewAdapter.onGetRightTopLayout(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            }
            mRightLLContainer.addView(mRightBottomRv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
    }

    public void notifuDataSetChanged() {
        mLeftBottomRVAdapter.notifyDataSetChanged();
        mRightBottomRVAdapter.notifyDataSetChanged();
    }

    /**
     * 跳转到初始位置
     */
    public void scrollToLeftTop() {
        mLeftBottomRv.scrollToPosition(0);
        mRightBottomRv.scrollToPosition(0);
        mRightRootContainer.scrollTo(0, 0);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        if (mLeftRecyclerTouchListener != null) {
            mLeftRecyclerTouchListener.setOnItemClickListener(onItemClickListener);
        }
        if (mRightRecyclerTouchListener != null) {
            mRightRecyclerTouchListener.setOnItemClickListener(onItemClickListener);
        }
    }

    private RecyclerView.Adapter mLeftBottomRVAdapter = new RecyclerView.Adapter() {
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return (mScrollTableViewAdapter == null || mScrollTableViewAdapter.onCreateLeftBottomViewHolder(parent, viewType) == null) ?
                    new NullViewHolder(new View(parent.getContext())) :
                    mScrollTableViewAdapter.onCreateLeftBottomViewHolder(parent, viewType);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (mScrollTableViewAdapter != null) {
                mScrollTableViewAdapter.onBindLeftBottomViewHolder(holder, position);
            }
        }

        @Override
        public int getItemCount() {
            return mScrollTableViewAdapter == null ? 0 : mScrollTableViewAdapter.getItemCount();
        }
    };

    private final RecyclerView.Adapter mRightBottomRVAdapter = new RecyclerView.Adapter() {
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return (mScrollTableViewAdapter == null || mScrollTableViewAdapter.onCreateRightBottomViewHolder(parent, viewType) == null) ?
                    new NullViewHolder(new View(parent.getContext())) :
                    mScrollTableViewAdapter.onCreateRightBottomViewHolder(parent, viewType);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (mScrollTableViewAdapter != null) {
                mScrollTableViewAdapter.onBindRightBottomViewHolder(holder, position);
            }
        }

        @Override
        public int getItemCount() {
            return mScrollTableViewAdapter == null ? 0 : mScrollTableViewAdapter.getItemCount();
        }
    };

    private static class NullViewHolder extends RecyclerView.ViewHolder {
        public NullViewHolder(View itemView) {
            super(itemView);
        }
    }
}
